import {createRouter, createWebHistory} from 'vue-router'
import LoginView from '../views/LoginView.vue'
import IndexView from '../views/IndexView.vue'
import {getToken} from '../api/token.js'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'index',
            component: IndexView,
        },
        {
            path: '/login',
            name: 'login',
            component: LoginView
        },
        {
            path: "/search", name: "search", component: ()=> import("../views/SearchView.vue")
        },
        {
            path: "/register",
            name: "register",
            component: () => import("../views/RegisterView.vue"),
        },
        {
            path: "/next",
            name: "next",
            component: () => import("../views/NextRegisterView.vue"),
        },
        {
            path: "/third",
            name: "third",
            component: () => import("../views/SuccessView.vue"),
        },
        {
            name: 'find-pass',
            path: '/find/pwd',
            component: () => import("../views/FindPwd.vue")
        },
        {
            name: 'resource-detail',
            path: '/detail/:id',
            component: () => import("../views/ResourceDetailView.vue"),
            props: true,
        },
        {
            name: 'catalog',
            path: '/catalog',
            component: () => import("../views/CatalogView.vue")
        },
        {
            name: 'personal',
            path: '/personal',
            component: () => import("../views/PersonalView.vue")
        },
        {
            name: 'upload',
            path: '/upload',
            component: () => import("../views/UploadResourceView.vue"),
            meta: {auth: true}
        },
        {
            name: 'collect',
            path: '/collect',
            component: () => import("../views/CollectResourceView.vue")
        },
        {
            path: "/modify",
            name: "modify-pwd",
            component: () => import("../views/ModifyPasswordView.vue")
        },
        {
            path: "/attention",
            name: "attention",
            component: () => import("../views/AttentionView.vue"),
            meta: {auth: true}
        },
        {
            path: "/more/:uid",
            name: "more",
            component: () => import("../views/MoreResourceView.vue"),
            props: true
        },
        {
            path: "/message",
            name: "message",
            component: () => import("../views/MessageView.vue"),
            props: route => ({type: route.query.type}) ,
            meta: {auth: true}
        },
        {
            path: "/alipay",
            name: "alipay",
            component: () => import("../views/AlipayView.vue"),
            meta: {auth: true}
        },
        {
            path: "/alipayOrder",
            name: "alipayOrder",
            component: () => import("../views/AlipayOrderView.vue"),
            meta: {auth: true}
        },
        {
            path: "/alipay/callback",
            name: "alipay-callback",
            component: () => import("../views/AlipayCallBackView.vue"),
            meta: {auth: true}
        },
        {
            path: "/order",
            name: "order",
            component: () => import("../views/OrderListView.vue"),
            meta: {auth: true}
        },
        {
            path: "/logout", name: "logout", component: () => import("../views/Logout.vue")
        },

        {
            path: "/authorize/callback" ,
            name: "authorize" ,
            component: () => import("../views/AuthorizeCallbackView.vue")
        },
        {
            name: "bind", path: "/bind" , component: ()=> import("../views/BindView.vue")
        },

    ]
})


router.beforeEach(async (to, from, next) => {
    try {
        if (to.path == "/login") {
            next(); return ;
        }
        let accessToken = await getToken();
        if (to.meta == null || !to.meta.auth || accessToken != null) {
            next();
        } else {
            next("/login");
        }
    } catch (e) {
        next("/login"); // 当接口调用出现异常，跳转到登录
    }
})

export default router
